let Class = function (vm) {
    this.vm = vm
}

/**
 * 用户信息
 */
Class.prototype.info = function () {
    return this.vm.$store.getters.getValue("vuex_user", null)
}

/**
 * 是否Guest用户
 */
Class.prototype.isGuest = function () {
    return !this.info() || !this.getToken()
}

/**
 * 是否超级用户
 * @returns {boolean}
 */
Class.prototype.isSuper = function () {
    return this.info() && this.info()['type'] === 9
}

/**
 * 存储Token
 */
Class.prototype.saveToken = function (token) {
    return this.vm.$store.dispatch("save", {name: "vuex_token", value: token})
}

/**
 * 获取token信息
 */
Class.prototype.getToken = function () {
    return this.vm.$store.getters.getValue("vuex_token", "")
}

/**
 * 存储用户信息
 */
Class.prototype.save = function (user) {
    return this.vm.$store.dispatch("save", {name: "vuex_user", value: user})
}


/**
 * 登录
 */
Class.prototype.login = function (user, token) {
    return this.vm.$store.dispatch("saveAll", [
        {name: "vuex_token", value: token},
        {name: "vuex_user", value: user}
    ])
}

/**
 * 注销
 */
Class.prototype.logout = function () {
    return this.vm.$store.dispatch("removeAll", [
        {name: "vuex_token"}, {name: "vuex_user"}
    ])
}

const install = (Vue, vm) => {
    Vue.prototype.$user = new Class(vm)
}

export default {
    install
}
